package com.imti.service.impl;

import com.imti.bean.DeskInfo;
import com.imti.bean.pojo.Dto;
import com.imti.bean.Nurse;
import com.imti.mapper.DoctorDao;
import com.imti.mapper.NuresDao;
import com.imti.service.NuresService;
import com.imti.util.PageBean;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class NuresServiceImpl implements NuresService {

    @Autowired
    private NuresDao nuresDao;

    @Autowired
    private DoctorDao doctorDao;

    @Override
    public PageBean pageNurse(Map<String, Object> map) {
        // 1、查询总条数
        Nurse nurse = (Nurse) map.get("nurse");
        PageBean pageBean = (PageBean) map.get("page");
        Integer pageLimt = (pageBean.getPage() == null) ? 5 : pageBean.getPage();
        Integer currNum = (pageBean.getCurrNo() == null) ? 1 : pageBean.getCurrNo();
        int totalCount = nuresDao.NurseConut(nurse);
        // 2、查询列表
        PageBean page = new PageBean(pageLimt, currNum);
        map.put("page", page);
        List<Dto> list = nuresDao.fllNurse(map);
        // 3、封装pagebean
        PageBean npage = new PageBean(pageLimt, totalCount, currNum, list, "/nurse/page", NurseParams(nurse));
        return npage;
    }
    /*新增*/
    @Override
    public int insert(Nurse nurse) {
        return nuresDao.insertNurse(nurse);
    }
    /*查询唯一*/
    @Override
    public int selectCountOne(Nurse nurse) {
        return nuresDao.selectCountOne(nurse);
    }

    @Override
    public int update(Nurse nurse) {
        return nuresDao.updateNurse(nurse);
    }


    private String NurseParams(Nurse nurse) {
        StringBuilder sb = new StringBuilder();
        if (StringUtils.isNotBlank(nurse.getNurseName())) {
            sb.append("&nurseName=").append(nurse.getNurseName());
        }
        if (StringUtils.isNotBlank(nurse.getState())) {
            sb.append("&state=").append(nurse.getState());
        }
        return sb.toString();
    }


  /*  @Override
    public void getDeskInfo(HttpServletRequest req) {
        List<DeskInfo> deskList=doctorDao.findAllDesk();
        req.setAttribute("deskList", deskList);
    }*/

    /*查询科室*/
    @Override
    public List<DeskInfo> findAllDesk() {
        return doctorDao.findAllDesk();
    }
}
